package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IErrorInfoDAO;
import org.jsola.hr.dto.ErrorInfoAddDTO;
import org.jsola.hr.dto.ErrorInfoUpdateDTO;
import org.jsola.hr.entity.ErrorInfoDO;
import org.jsola.hr.query.ErrorInfoQuery;
import org.jsola.hr.service.IErrorInfoService;
import org.jsola.hr.vo.ErrorInfoListVO;
import org.jsola.hr.vo.ErrorInfoVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 异常记录表
 *
 * @author zhr
 */
@Service("hrErrorInfoServiceImpl")
public class ErrorInfoServiceImpl implements IErrorInfoService {

    @Autowired
    private IErrorInfoDAO errorInfoDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public ErrorInfoVO save(ErrorInfoAddDTO errorInfoAddDTO, TokenUser tokenUser) {
        // 转DO
        ErrorInfoDO errorInfoDO = errorInfoAddDTO.to(ErrorInfoDO.class);
        // 保存
        errorInfoDO = save(errorInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return errorInfoDO.to(ErrorInfoVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(ErrorInfoUpdateDTO errorInfoUpdateDTO, TokenUser tokenUser) {
        //转DO
        ErrorInfoDO errorInfoDO = errorInfoUpdateDTO.to(ErrorInfoDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(errorInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... errorInfoIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) errorInfoIds);
    }


    @Override
    public ErrorInfoVO selectById(String errorInfoId, String siteId) {
        ErrorInfoDO errorInfoDO = selectDOById(errorInfoId, siteId);
        if (errorInfoDO == null) {
            return null;
        }
        return errorInfoDO.to(ErrorInfoVO.class);
    }

    @Override
    public List<ErrorInfoListVO> select(ErrorInfoQuery errorInfoQuery, String siteId) {
        List<ErrorInfoDO> errorInfoDOList = selectDO(errorInfoQuery, siteId);
        if(CollectionUtils.isEmpty(errorInfoDOList)) {
            return errorInfoDOList == null ? null : new ArrayList<>();
        }
        return errorInfoDOList.stream()
                .map(errorInfoDO -> errorInfoDO.to(ErrorInfoListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ErrorInfoQuery errorInfoQuery, String siteId) {
        Example example = buildExample(errorInfoQuery,siteId);
        return errorInfoDAO.selectCountByExample(example);
    }

    @Override
    public Page<ErrorInfoListVO> selectPage(ErrorInfoQuery errorInfoQuery, String siteId) {
        Example example = buildExample(errorInfoQuery,siteId);
        Page<ErrorInfoDO> page = errorInfoDAO.selectPageByExample(example,
                errorInfoQuery.getPageNo(),
                errorInfoQuery.getPageSize());

        return page.to(ErrorInfoListVO.class);
    }

    @Override
    public ErrorInfoDO selectDOById(String errorInfoId, String siteId) {
        return listById(errorInfoId, siteId);
    }

    @Override
    public List<ErrorInfoDO> selectDO(ErrorInfoQuery errorInfoQuery, String siteId) {
        Example example = buildExample(errorInfoQuery,siteId);
        return errorInfoDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param errorInfoQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(ErrorInfoQuery errorInfoQuery, String siteId) {
        Example example = new Example(ErrorInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,errorInfoQuery.getOrders());
        return example;
    }
}




